/**
 * 
 */
package com.logistics.web;

import java.util.Date;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.logistics.Courier;
import com.logistics.Express;
import com.logistics.ExpressCompany;
import com.logistics.ExpressProperty;
import com.logistics.ExpressPropertyQueryParam;
import com.logistics.TimeTemplate;
import com.logistics.Tips;
import com.logistics.admin.BaseAction;
import com.logistics.admin.DateUtils;
import com.logistics.service.CourierService;
import com.logistics.service.ExpressCompanyService;
import com.logistics.service.ExpressPropertyService;
import com.logistics.service.ExpressService;
import com.logistics.service.TimeTemplateService;
import com.logistics.service.TipsService;

/**
 * @author hzz
 *
 * 2014年4月2日
 */

@Component("expressAction")
@Scope("prototype")
public class ExpressAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private ExpressService service;
	
	public ExpressAction() {
		super(false);
	}
	
	@Autowired
	public void setService(ExpressService service) {
		this.service = service;
	}
	
	private ExpressCompanyService companyService;
	
	@Autowired
	public void setCompanyService(ExpressCompanyService companyService) {
		this.companyService = companyService;
	}
	
	private ExpressPropertyService propertyService;
	
	@Autowired
	public void setPropertyService(ExpressPropertyService propertyService) {
		this.propertyService = propertyService;
	}
	
	private CourierService courierService;

	@Autowired
	public void setCourierService(CourierService courierService) {
		this.courierService = courierService;
	}

	private TimeTemplateService templateService;

	@Autowired
	public void setTemplateService(TimeTemplateService templateService) {
		this.templateService = templateService;
	}
	
	private Express express;

	public Express getExpress() {
		return express;
	}

	public void setExpress(Express express) {
		this.express = express;
	}
	
	private List<TimeTemplate> templates;
	
	public List<TimeTemplate> getTemplates() {
		return templates;
	}

	public void setTemplates(List<TimeTemplate> templates) {
		this.templates = templates;
	}
	
	private List<ExpressCompany> companies;
	
	public List<ExpressCompany> getCompanies() {
		return companies;
	}

	public void setCompanies(List<ExpressCompany> companies) {
		this.companies = companies;
	}
	
	private List<ExpressProperty> properties;

	public List<ExpressProperty> getProperties() {
		return properties;
	}

	public void setProperties(List<ExpressProperty> properties) {
		this.properties = properties;
	}

	private List<Courier> couriers;
	
	public List<Courier> getCouriers() {
		return couriers;
	}

	public void setCouriers(List<Courier> couriers) {
		this.couriers = couriers;
	}
	
	private Tips tips;
	
	public Tips getTips() {
		return tips;
	}

	public void setTips(Tips tips) {
		this.tips = tips;
	}

	private TipsService tipsService;
	
	@Autowired
	public void setTipsService(TipsService tipsService) {
		this.tipsService = tipsService;
	}

	private String id;
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String showAddView() {
		templates = templateService.queryAvailable(true);
		companies =  companyService.query(null);
		couriers = courierService.query(null);
		tips = tipsService.getById("1");
		return SUCCESS;
	}
	
	public String save() {
		express.setCreateDate(new Date());
		express.setRemind(true);
		express.setPrint(true);
		service.save(express);
		
		return SUCCESS;
	}
	
	public String expressInfo() {
		if (StringUtils.isBlank(id)) {
			return "showAdd";
		}
		express = service.getById(id);
		return SUCCESS;
	}
	
	private String companyId;
	
	public String getCompanyId() {
		return companyId;
	}

	public void setCompanyId(String companyId) {
		this.companyId = companyId;
	}

	public void getPropertyForJson() {
		ExpressPropertyQueryParam param = new ExpressPropertyQueryParam();
		param.setCompanyId(companyId);
		properties = propertyService.query(param);
		outputJsonWithRecordset(JSONArray.fromObject(properties));
	}
	
	public void needPrints() {
		List<Express> expresses =  service.needPrints();
		JSONObject jsonObject = new JSONObject();
		StringBuffer buffer = new StringBuffer();
		for (Express express : expresses) {
			if (StringUtils.isNotBlank(buffer.toString())) {
				buffer.append("," + express.getEntityId());
			} else {
				buffer.append(express.getEntityId());
			}
		}
		jsonObject.put("status", 200);
		jsonObject.put("ids", buffer.toString());
		outputString(jsonObject.toString());
		
	}
	
	public void printDetail() {
		if (StringUtils.isBlank(getId())) {
			outputInvalidStatus(500);
			return;
		}
		
		express = service.getById(getId());
		JsonConfig config = getJsonConfig();
		
		if (express == null || "0".equals(express.getEntityId())) {
			outputInvalidStatus(500);
			return;
		}
		
		express.setPrint(false);
		service.update(express);
		
		config.registerJsonValueProcessor("createDate", new JsonValueProcessor() {
			
			@Override
			public Object processObjectValue(String arg0, Object arg1, JsonConfig arg2) {
				
				return DateUtils.formatDateTime((Date) arg1);
			}
			
			@Override
			public Object processArrayValue(Object arg0, JsonConfig arg1) {
				return null;
			}
		});
		
		outputJsonObject(JSONObject.fromObject(express, config));
	}
	
}
